#ifndef SCOPE_H
#define SCOPE_H

#include "scopeNode.h"
using namespace std;

//作用域
class Scope
{
private:
    stack<ScopeNode> scopes;

public:
    //添加Scope
    void pushScope(ScopeNode scope);
    //整个块结束,弹出该块
    void popScope();
    //查看最顶层的块
    ScopeNode topScope();
};
ScopeNode::ScopeNode(string _name, ScopeNode *_parentNode) : name(_name), parentNode(_parentNode)
{
    if (this->parentNode == NULL)
        this->depth = 0;
    else
        this->depth = this->parentNode->depth + 1;
};

bool ScopeNode::searchSymbol(string symbol)
{
    if (variables.find(symbol))
    {
        return true;
    }
    else
    {
        //一直向父作用域寻找该变量,直到最外层作用域为止
        ScopeNode *node = this->parentNode;
        while (node != NULL)
        {
            if (node->variables.find(symbol))
            {
                return true;
            }
            else
            {
                node = node->parentNode;
            }
        }
        return false;
    }
}

//插入符号，在域符号表中插入符号
bool ScopeNode::insertSymbol(string symbol, SymbolTableNode symbolNode)
{
    if (!searchSymbol(symbol))
    {
        variables.insert(symbol, symbolNode);
        return true;
    }
    else
    {
        return false;
    }
}

#endif